package test.jdbc;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;


/**
 * @description JDBC工具集
 *
 * @author linshuo
 *
 */
public class JdbcUtils {
    private static Properties props = null;
    /*加载数据库驱动在一个程序中只需要一次*/
    static {
        try {

            InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("dbconfig.properties");
            props = new Properties();
            props.load(in);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        try {
            Class.forName(props.getProperty("driverClassName"));
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static Connection getConnection() throws SQLException {
        String urlStr = props.getProperty("url");
        String usernameStr = props.getProperty("username");
        String passwordStr = props.getProperty("password");
        return DriverManager.getConnection(urlStr, usernameStr, passwordStr);
    }
}
